package io.renren.modules.sys.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.SysDept1Entity;
import io.renren.modules.sys.entity.SysRoleEntity;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.*;
import io.renren.modules.sys.shiro.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.entity.SysRole1Entity;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 角色
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-08-13 20:51:21
 */
@RestController
@RequestMapping("sys/sysrole1")
public class SysRole1Controller {
    @Autowired
    private SysRole1Service sysRole1Service;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private SysDept1Service sysDept1Service;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:sysrole1:list")
    public R list(@RequestParam Map<String, Object> params){

        Long userId= ShiroUtils.getUserId();
        SysUserEntity sysUserEntity=sysUserService.getById(userId);
        List<Long> sysUserRoleEntityList=(List)sysUserRoleService.queryRoleIdList(userId);
        System.out.print("roleid is "+sysUserRoleEntityList.get(0)+"*****************");
        Integer parentid=sysUserRoleEntityList.get(0).intValue();
//        SysRole1Entity sysRole1Entity=sysRole1Service.getById(roleId);
//        Integer parentid=sysRole1Entity.getParentId();
        params.put("parentid",parentid);
        PageUtils page = sysRole1Service.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 角色列表
     */
    @RequestMapping("/select")
    @RequiresPermissions("sys:sysrole1:select")
    public R select(){

        Long userId= ShiroUtils.getUserId();
        SysUserEntity sysUserEntity=sysUserService.getById(userId);
        List<Long> sysUserRoleEntityList=(List)sysUserRoleService.queryRoleIdList(userId);
        System.out.print("roleid is "+sysUserRoleEntityList.get(0)+"*****************");
        Integer parentid=sysUserRoleEntityList.get(0).intValue();
        QueryWrapper<SysRole1Entity>  queryWrapper=new QueryWrapper<SysRole1Entity>().eq("parent_id",parentid);
        List<SysRole1Entity> list = sysRole1Service.list(queryWrapper);

        return R.ok().put("list", list);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{roleId}")
    @RequiresPermissions("sys:sysrole1:info")
    public R info(@PathVariable("roleId") Long roleId){
        SysRole1Entity sysRole1 = sysRole1Service.getById(roleId);
        //查询角色对应的菜单
        List<Long> menuIdList = sysRoleMenuService.queryMenuIdList(roleId);
        sysRole1.setMenuIdList(menuIdList);

        return R.ok().put("sysRole1", sysRole1);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:sysrole1:save")
    public R save(@RequestBody SysRole1Entity sysRole1){
        //sysRole1Service.save(sysRole1);
        System.out.println(sysRole1);
        Long userId= ShiroUtils.getUserId();

        SysUserEntity sysUserEntity=sysUserService.getById(userId);
        List<Long> sysUserRoleEntityList=(List)sysUserRoleService.queryRoleIdList(userId);
        System.out.print("roleid is " + sysUserRoleEntityList.get(0) + "*****************");
        Integer roleId=sysUserRoleEntityList.get(0).intValue();
        sysRole1.setParentId(roleId);

        Integer rolerank=sysRole1Service.queryRoleRank(roleId);
        System.out.print("rolerank is " + rolerank + "*****************");
        if (rolerank==1)
            sysRole1.setRoleRank(2);
        if(rolerank==2)
            sysRole1.setRoleRank(3);
        if(rolerank==3)
            sysRole1.setRoleRank(4);
        ValidatorUtils.validateEntity(sysRole1);

        sysRole1Service.saveRole1(sysRole1);


        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:sysrole1:update")
    public R update(@RequestBody SysRole1Entity sysRole1){
        ValidatorUtils.validateEntity(sysRole1);
        //sysRole1Service.updateById(sysRole1);
        sysRole1Service.update1(sysRole1);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:sysrole1:delete")
    public R delete(@RequestBody Long[] roleIds){
        //sysRole1Service.removeByIds(Arrays.asList(roleIds));
        sysRole1Service.deleteBatch1(roleIds);

        return R.ok();
    }

}
